/**未遂事故的列表页面 */
<template>
  <div class="app-container">

    <!-- Note that row-key is necessary to get a correct row order. -->
    <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">

      <el-table-column width="180px" align="center" label="未遂事故单编号" prop="faultId" />

      <el-table-column min-width="300px" label="未遂事故题目">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.faultTitle }}</span></template>
      </el-table-column>
      <el-table-column min-width="300px" label="未遂事故类型">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.fault_TypeId }}</span></template>
      </el-table-column>
      <el-table-column width="110px" align="center" label="发生地点">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.faultLocation }}</span></template>
      </el-table-column>
      <el-table-column width="100px" label="发生时间">

        <template slot-scope="scope">

          <span>{{ scope.row.faultTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span></template>
      </el-table-column>
      <el-table-column align="center" label="发现人" width="95">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.faultUserCode.userName }}</span></template>
      </el-table-column>
      <el-table-column class-name="status-col" label="所属Team" width="110">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.faultTeam.deptName }}</span></template>
      </el-table-column>
      <el-table-column class-name="status-col" label="附件" width="110">
        <template v-if="scope.row.faultFileId==1" slot-scope="scope">
          <el-button type="primary" size="small" @click="handlegetFile(scope.row)">附件</el-button>
        </template></el-table-column>
      <el-table-column align="center" label="裁决状态" width="80" prop="faultSatus">
        <template slot-scope="scope">
          <span v-if="scope.row.faultSatus==1">申请中</span>
          <span v-if="scope.row.faultSatus==0">已删除</span>
          <span v-if="scope.row.faultSatus==2">已裁决</span>
        </template>
      </el-table-column>

    </el-table>
    <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fechData" /> -->

  </div>
</template>

<script>
// import ElDragSelect from '@/components/DragSelect' // base on element-ui
// import MarkdownEditor from '@/components/MarkdownEditor'
import { fetchFaultList1, fetchAllCount } from '@/api/fault'
import Sortable from 'sortablejs'
import { parseTime } from '@/utils'
import waves from '@/directive/waves' // waves directive
// import Pagination from '@/components/Pagination' // secondary package based on el-pagination
// import { getuserbyemp } from '@/api/user'
import { getFileName } from '@/api/file'

export default {
  name: 'Fault',
  // components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }

  },
  data() {
    return {
      id: '',
      type: 'ehs_fault',
      file: null,
      limitNum: 1,
      formLabelWidth: '80px',
      // form: {
      //   file: '',
      //  file1: ''
      // },
      // filesId: '',
      fileList: [],
      faultType: [],
      options: [{
        value: '不注意',
        label: '不注意'
      }, {
        value: '未遵守章程',
        label: '未遵守章程'
      }, {
        value: '管理不当',
        label: '管理不当'
      }, {
        value: '操作失误',
        label: '操作失误'
      }, {
        value: '设备缺陷',
        label: '设备缺陷'
      }, {
        value: '信息共享',
        label: '信息共享'
      }, {
        value: '其他',
        label: '其他'
      }],
      faultType1: null,
      list: null,
      total: 0,
      listLoading: true,
      editList: null,
      deptName: null,
      userName: null,
      deptId: null,
      userId: null,
      sortable: null,
      oldList: [],
      newList: [],
      // 参数条件
      listQuery: {
        page: 1,
        limit: 10,
        faultTitle: null,
        faultLocation: null,
        startDate: null,
        endDate: null
      },
      personlist: null,
      deptlist: null,

      temp: {
        faultId: '',
        faultTitle: '',
        faultLocation: '',
        faultContent: '',
        faultReason: '',
        faultFun: '',
        faultRemark: '',
        faultadvice: '',
        faultWriteTime: new Date(),
        faultTime: new Date(),
        faultSatus: 1,
        faultTeam: '',
        faultFileId: '',
        faultUserCode: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增',
        detail: '明细'
        // score: '评价'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        // timestamp: [{ type: 'faultTime', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.fechData()
  },
  methods: {
    fechData() {
      // getPersonList().then(response => {
      //   this.personlist = response.listName
      // })
      // getDeptList().then(response => {
      //   this.deptlist = response.listName
      // })
      fetchAllCount().then(response => {
        this.total = response.count
      })
      fetchFaultList1(this.listQuery).then(response => {
        this.listLoading = true
        this.list = response.ehsfaultList

        this.showList = response.ehsfaultList
        // .log('888888888888888' + response.ehsfaultList)
        this.listLoading = false
        // this.oldList = response.map(v => v.id)
      //  this.newList = response.map(v => v.id).slice()
      })
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        }
        if (j === 'faultTime') {
          return parseTime(v[j])
        }
        if (j === 'faultWriteTime') {
          return parseTime(v[j])
        }
        if (j === 'faultUserCode') {
          return v[j].userName
        } if (j === 'faultTeam') {
          return v[j].deptName
        } if (j === 'fault_TypeId') {
          return v[j]
        } else {
          return v[j]
        }
      }))
    },
    showReviewer() {

    },

    handleFilter() {
      this.listQuery.page = 1
      this.fechData()
    },
    resetTemp() {
      this.temp = {
        faultId: undefined,

        faultTitle: '',
        faultLocation: '',
        faultContent: '',
        faultReason: '',
        faultFun: '',
        faultRemark: '',
        faultadvice: '',
        faultWriteTime: new Date(),
        faultTime: new Date(),
        faultSatus: 1,
        faultTeam: '',
        faultFileId: '',
        faultUserCode: ''
      }
    },

    handlegetFile(row) {
      // alert(row.faultId)
      this.id = row.faultId
      this.type = 'ehs_fault'
      // // 1.先创建一个a标签
      // const a = document.createElement('a')
      // // 2.给a标签的href赋上下载地址
      // a.href = 'http://localhost:8080/ehs_files/getfile?id=' + this.id + '&type=' + this.type
      // // 3.让a标签不显示在视图中
      // a.style.display = 'none'
      // // 4.将a标签append到文档中
      // document.body.appendChild(a)
      // // a.setAttribute('download', a.fileName)
      // a.download = '附件'
      // console.log('MMMMMMMMMMMMMMMMMMMM' + a)
      // // 5.a标签自点击
      // a.click()
      // // 6.点击下载后，销毁这个节点
      // document.body.removeChild(a)

      const query = { id: this.id, type: this.type }
      // const query1 = JSON.parse(JSON.stringify(query))
      console.log('query is  ' + query)
      // 获取附件名称

      getFileName(query).then(response => {
        this.file = response.file
        console.log('MMMMMMMMMMMMMMMMMMMMMMMM' + this.file)
        const url = 'http://localhost:8080/ehs_files/getfile?id=' + this.id + '&type=' + this.type // 创建下载链接
        const link = document.createElement('a') // 创建a标签
        link.style.display = 'none' // 将a标签隐藏
        link.href = url // 给a标签添加下载链接
        link.setAttribute('download', this.file.filename) // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
        document.body.appendChild(link)
        link.click() // 执行a标签
      })
      // getFile(query).then(response => {
      //   const cookie = response.headers.cookie
      //   console.log(cookie)
      //   this.file1 = response.file

      // })
    },

    getHtml() {
      this.faultContent = this.$refs.markdownEditor.getHtml()
      // console.log(this.faultContent)
    },

    setSort() {
      const el = this.list[0]

      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
        setData: function(dataTransfer) {
          // to avoid Firefox bug
          // Detail see : https://github.com/RubaXa/Sortable/issues/1012
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)

          // for show the changes, you can delete in you code
          const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
          this.newList.splice(evt.newIndex, 0, tempIndex)
        }
      })
    },
    handleChange() {

    },
    uploadfile(response, file, fileList) {
      // alert('111')
      this.file = response.file
    }

  }

}
</script>

<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}
</style>
